class Register {
    constructor() {
        //获取节点
        this.text = document.querySelector('input[name="text"]')
        this.pass1 = document.querySelector('input[name="password1"]')
        this.pass2 = document.querySelector('input[name="password2"]')
        this.btn = document.querySelectorAll('button')
        this.p = document.querySelectorAll('p')
        //设置正侧
        this.name_zc = /^[a-z0-9_-]{3,16}$/
        this.pass_zc = /^[a-z0-9_-]{6,18}$/
        //初始化
        this.init()
    }
    init() {
        this.user()
        this.login()
    }
    user(){
        let obj = {}
        this.click(obj)
    }
    //绑定点击事件
    click(obj) {
        this.btn[0].onclick = (e) => {
            //阻止默认事件
            e.preventDefault()
            //获取输入框的值
            let text_val = this.text.value
            let pass1_val = this.pass1.value
            let pass2_val = this.pass2.value

            //先正则，然后判定密码是否一样
            //正侧验证账号
            if (text_val == '') {
                this.p[0].innerHTML = '账号不能为空'
                return
            } else {
                this.p[0].innerHTML = ''
            }

            if (this.name_zc.test(text_val)) {
                this.p[0].innerHTML = ''
            } else {
                this.p[0].innerHTML = '请正确输入账号'
                return
            }

            //正侧验证密码
            if (pass1_val == '') {
                this.p[1].innerHTML = '密码不能为空'
                return
            } else {
                this.p[1].innerHTML = ''
            }

            if (this.pass_zc.test(pass1_val)) {
                this.p[1].innerHTML = ''
            } else {
                this.p[1].innerHTML = '请规范输入密码'
                return
            }

            if (pass2_val == '') {
                this.p[2].innerHTML = '此处不能为空'
                return
            } else {
                this.p[2].innerHTML = ''
            }
            if (pass1_val !== pass2_val) {
                this.p[2].innerHTML = '密码不一致，请重新输入'
                return
            } else {
                this.p[2].innerHTML = ''
            }
            obj.name = text_val
            obj.pass = pass1_val
            //查看localstorage里面有没有相同的账号
            let key_name = JSON.parse(localStorage.getItem('username')) || []
              if(!key_name.length){
                 key_name.push(obj)
                 alert('注册成功')
              }else{
                  let ser = key_name.some(item=>{
                     return item.name == text_val
                   })
                   if(ser){
                       this.p[2].innerHTML = '此账号已被注册，请重新注册'
                       return
                   }else{
                    key_name.push(obj)
                    alert('注册成功')
                   }
              }
              console.log(key_name);
              //把通过的存储数据
              localStorage.setItem('username',JSON.stringify(key_name))
        }
    }
    login(){
        this.btn[1].onclick =(e)=>{
            //阻止默认事件
            e.preventDefault()
            location.href = '/html/login.html'
        }
    }
}
new Register()